#include "gui.h"
#include "admin.h"
#include "common.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <glib.h>
#include <stdlib.h>
#include <gtk/gtk.h>  // 只保留一个GTK头文件包含，放在开头

// 函数声明
static void on_login_clicked(GtkButton *button, gpointer data);
static void on_member_submit(GtkButton *button, gpointer data);
static void on_modify_clicked(GtkButton *button, gpointer data);
static void on_modify_save_clicked(GtkButton *button, gpointer data);
static void on_delete_clicked(GtkButton *button, gpointer data);
static void on_list_all_clicked(GtkButton *button, gpointer data);
static GtkWidget* create_combo(const gchar* const* options);
static void update_generated_id_label();
static void on_card_id_changed(GtkEditable *editable, gpointer data);
static gboolean validate_birth_date(int year, int month, int day);
static gboolean find_user_by_id(unsigned int user_id, user_imf *user);
static gboolean find_user_by_card_id(const char *card_id, user_imf *user);
static GSList* find_users_by_name(const char *name);
void on_query_clicked(GtkButton *button, gpointer data);  // 修正声明

// 全局控件指针
static GtkWidget *current_window;
static GtkWidget *username_entry;
static GtkWidget *password_entry;
static GtkWidget *query_result_text;
static GtkWidget *generated_id_label;
static GtkWidget *card_id_entry, *name_entry;
static GtkWidget *sex_combo, *birth_calendar;
static GtkWidget *phone_entry;
static GtkWidget *type_combo, *balance_entry, *status_combo;
static GtkWidget *list_text_view;
static GtkWidget *query_input_entry;
static GtkWidget *query_type_combo;
static GtkWidget *modify_dialog;
static GtkWidget *modify_card_id_entry, *modify_name_entry;
static GtkWidget *modify_sex_combo, *modify_birth_calendar;
static GtkWidget *modify_phone_entry;
static GtkWidget *modify_type_combo, *modify_balance_entry, *modify_status_combo;
static unsigned int current_modify_id;
static GtkWidget *query_button;
static GtkWidget *hall_grid;  // 添加hall_grid的全局声明

// 生成唯一用户ID
static unsigned int generate_user_id() {
    FILE* fp = fopen("./member_data.dat", "rb");
    if (!fp) return 10000;  // 初始ID
    
    unsigned int max_id = 10000;
    user_imf user;
    while (fread(&user, sizeof(user_imf), 1, fp) == 1) {
        if (user.user_id > max_id) {
            max_id = user.user_id;
        }
    }
    fclose(fp);
    return max_id + 1;
}

// 更新系统生成ID标签
static void update_generated_id_label() {
    unsigned int new_id = generate_user_id();
    char id_str[20];
    snprintf(id_str, sizeof(id_str), "%u", new_id);
    gtk_label_set_text(GTK_LABEL(generated_id_label), id_str);
}

// 卡号输入长度检查回调
static void on_card_id_changed(GtkEditable *editable, gpointer data) {
    const char *text = gtk_entry_get_text(GTK_ENTRY(editable));
    int len = strlen(text);
    if (len > 10) {
        char *truncated = g_strndup(text, 10);
        gtk_entry_set_text(GTK_ENTRY(editable), truncated);
        g_free(truncated);
        show_message_dialog(GTK_WINDOW(current_window), "卡号最多10位！");
    }
}

// 生日日期验证
static gboolean validate_birth_date(int year, int month, int day) {
    // 检查年份是否在合理范围内
    if (year < 1900 || year > 2100) return FALSE;
    
    // 检查月份
    if (month < 1 || month > 12) return FALSE;
    
    // 检查日期
    if (day < 1) return FALSE;
    
    // 检查每月的最大天数
    switch (month) {
        case 2:
            // 处理闰年
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                return day <= 29;
            } else {
                return day <= 28;
            }
        case 4: case 6: case 9: case 11:
            return day <= 30;
        default:
            return day <= 31;
    }
}

// 从ID查找用户
static gboolean find_user_by_id(unsigned int user_id, user_imf *user) {
    FILE* fp = fopen("./member_data.dat", "rb");
    if (!fp) return FALSE;
    
    user_imf temp_user;
    while (fread(&temp_user, sizeof(user_imf), 1, fp) == 1) {
        if (temp_user.user_id == user_id) {
            *user = temp_user;
            fclose(fp);
            return TRUE;
        }
    }
    
    fclose(fp);
    return FALSE;
}

// 从卡号查找用户
static gboolean find_user_by_card_id(const char *card_id, user_imf *user) {
    FILE* fp = fopen("./member_data.dat", "rb");
    if (!fp) return FALSE;
    
    user_imf temp_user;
    while (fread(&temp_user, sizeof(user_imf), 1, fp) == 1) {
        if (strcmp(temp_user.card_id, card_id) == 0) {
            *user = temp_user;
            fclose(fp);
            return TRUE;
        }
    }
    
    fclose(fp);
    return FALSE;
}

// 从姓名查找用户（模糊匹配）
static GSList* find_users_by_name(const char *name) {
    FILE* fp = fopen("./member_data.dat", "rb");
    if (!fp) return NULL;
    
    GSList *results = NULL;
    user_imf temp_user;
    
    while (fread(&temp_user, sizeof(user_imf), 1, fp) == 1) {
        if (strstr(temp_user.user_name, name) != NULL) {
            user_imf *user_copy = g_new(user_imf, 1);
            *user_copy = temp_user;
            results = g_slist_append(results, user_copy);
        }
    }
    
    fclose(fp);
    return results;
}

// 登录按钮回调
static void on_login_clicked(GtkButton *button, gpointer data) {
    const char *username = gtk_entry_get_text(GTK_ENTRY(username_entry));
    const char *password = gtk_entry_get_text(GTK_ENTRY(password_entry));
    
    if (admin_login(username, password)) {
        GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(current_window),
                                                 GTK_DIALOG_MODAL,
                                                 GTK_MESSAGE_INFO,
                                                 GTK_BUTTONS_OK,
                                                 "登录成功！");
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        
        GtkApplication *app = GTK_APPLICATION(gtk_window_get_application(GTK_WINDOW(current_window)));
        gtk_widget_destroy(current_window);
        show_main_window(app, username);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(current_window),
                                                 GTK_DIALOG_MODAL,
                                                 GTK_MESSAGE_ERROR,
                                                 GTK_BUTTONS_OK,
                                                 "用户名或密码错误！");
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 创建登录窗口
void show_login_window(GtkApplication *app) {
    current_window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(current_window), "台球会员管理系统 - 登录");
    gtk_window_set_default_size(GTK_WINDOW(current_window), 420, 320);
    gtk_window_set_position(GTK_WINDOW(current_window), GTK_WIN_POS_CENTER);
    
    // 加载CSS样式
    GtkCssProvider *css = gtk_css_provider_new();
    gtk_css_provider_load_from_data(css,
        ".login-container { background-color: #f5f5f5; border-radius: 10px; padding: 30px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }"
        ".title { font-size: 20pt; font-weight: bold; color: #2c3e50; margin-bottom: 30px; }"
        ".login-button { background-color: #3498db; color: white; padding: 10px 24px; border-radius: 5px; font-size: 11pt; border: none; }"
        ".login-button:hover { background-color: #2980b9; }"
        "GtkEntry { padding: 10px; border-radius: 5px; border: 1px solid #bdc3c7; font-size: 10pt; }"
        "GtkLabel { font-size: 11pt; color: #34495e; }",
        -1, NULL);
    gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
                                             GTK_STYLE_PROVIDER(css),
                                             GTK_STYLE_PROVIDER_PRIORITY_USER);
    g_object_unref(css);
    
    // 主容器
    GtkWidget *main_container = gtk_frame_new(NULL);
    gtk_widget_set_name(main_container, "login-container");
    gtk_container_set_border_width(GTK_CONTAINER(main_container), 10);
    
    GtkWidget *main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(main_container), main_box);
    
    // 标题
    GtkWidget *title_label = gtk_label_new("野马台球俱乐部会员管理系统");
    gtk_widget_set_name(title_label, "title");
    gtk_box_pack_start(GTK_BOX(main_box), title_label, FALSE, FALSE, 10);
    gtk_widget_set_halign(title_label, GTK_ALIGN_CENTER);
    
    // 登录表单容器
    GtkWidget *form_grid = gtk_grid_new();
    gtk_grid_set_row_spacing(GTK_GRID(form_grid), 15);
    gtk_grid_set_column_spacing(GTK_GRID(form_grid), 15);
    gtk_box_pack_start(GTK_BOX(main_box), form_grid, FALSE, FALSE, 10);
    
    // 用户名输入
    GtkWidget *username_label = gtk_label_new("用户名:");
    gtk_widget_set_halign(username_label, GTK_ALIGN_END);
    username_entry = gtk_entry_new();
    gtk_entry_set_placeholder_text(GTK_ENTRY(username_entry), "请输入管理员账号");
    gtk_widget_set_size_request(username_entry, 200, -1);
    gtk_grid_attach(GTK_GRID(form_grid), username_label, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), username_entry, 1, 0, 1, 1);
    
    // 密码输入
    GtkWidget *password_label = gtk_label_new("密码:");
    gtk_widget_set_halign(password_label, GTK_ALIGN_END);
    password_entry = gtk_entry_new();
    gtk_entry_set_visibility(GTK_ENTRY(password_entry), FALSE);
    gtk_entry_set_input_purpose(GTK_ENTRY(password_entry), GTK_INPUT_PURPOSE_PASSWORD);
    gtk_entry_set_placeholder_text(GTK_ENTRY(password_entry), "请输入管理员密码");
    gtk_widget_set_size_request(password_entry, 200, -1);
    gtk_grid_attach(GTK_GRID(form_grid), password_label, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), password_entry, 1, 1, 1, 1);
    
    // 登录按钮
    GtkWidget *login_button = gtk_button_new_with_label("登录");
    gtk_widget_set_size_request(login_button, 120, 40);
    gtk_widget_set_name(login_button, "login-button");
    g_signal_connect(login_button, "clicked", G_CALLBACK(on_login_clicked), NULL);
    
    // 按钮容器
    GtkWidget *button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_widget_set_halign(button_box, GTK_ALIGN_CENTER);
    gtk_box_pack_start(GTK_BOX(button_box), login_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(main_box), button_box, FALSE, FALSE, 30);
    
    gtk_container_add(GTK_CONTAINER(current_window), main_container);
    gtk_widget_show_all(current_window);
}

// 创建下拉框控件
static GtkWidget* create_combo(const gchar* const* options) {
    GtkWidget *combo = gtk_combo_box_text_new();
    for (int i = 0; options[i]; i++) {
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), options[i]);
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
    gtk_widget_set_size_request(combo, 200, 35);
    return combo;
}

// 提交会员表单回调
static void on_member_submit(GtkButton *button, gpointer data) {
    user_imf member = {0};
    
    // 获取系统生成的用户ID
    const char *id_text = gtk_label_get_text(GTK_LABEL(generated_id_label));
    if (sscanf(id_text, "%u", &member.user_id) != 1) {
        show_message_dialog(GTK_WINDOW(current_window), "获取用户ID失败！");
        return;
    }
    
    // 从表单获取数据
    const char* card_id = gtk_entry_get_text(GTK_ENTRY(card_id_entry));
    const char* name = gtk_entry_get_text(GTK_ENTRY(name_entry));
    int sex_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(sex_combo));
    
    // 从日历选择器获取生日
    guint year, month, day;
    gtk_calendar_get_date(GTK_CALENDAR(birth_calendar), &year, &month, &day);
    month++; // GTK的月份是0-11，需要转换为1-12
    
    char birth_str[11] = "";
    if (validate_birth_date(year, month, day)) {
        snprintf(birth_str, sizeof(birth_str), "%04d-%02d-%02d",
                 year, month, day);
    }
    
    const char* phone = gtk_entry_get_text(GTK_ENTRY(phone_entry));
    int type_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(type_combo));
    const char* balance_str = gtk_entry_get_text(GTK_ENTRY(balance_entry));
    int status_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(status_combo));
    
    // 验证必填字段
    if (strlen(name) == 0) {
        show_message_dialog(GTK_WINDOW(current_window), "姓名为必填项！");
        return;
    }
    if (strlen(card_id) != 10) {
        show_message_dialog(GTK_WINDOW(current_window), "卡号必须为10位！");
        return;
    }
    if (!is_card_id_unique(card_id, 0)) {
        show_message_dialog(GTK_WINDOW(current_window), "卡号已存在！");
        return;
    }
    
    // 填充会员信息结构体
    strncpy(member.card_id, card_id, sizeof(member.card_id)-1);
    strncpy(member.user_name, name, sizeof(member.user_name)-1);
    member.sex = sex_idx == 0 ? male : female;
    strncpy(member.birth, birth_str, sizeof(member.birth)-1);
    strncpy(member.phone, phone, sizeof(member.phone)-1);
    
    // 记录注册时间（精确到秒）
    member.reg_date = time(NULL);
    
    member.user_type = type_idx;
    member.balance = atoi(balance_str);
    member.status = status_idx;
    
    // 设置过期时间
    time_t now = time(NULL);
    struct tm* tm = localtime(&now);
    if (tm == NULL) {
        show_message_dialog(GTK_WINDOW(current_window), "时间处理错误！");
        return;
    }
    
    switch (member.user_type) {
        case month_card: tm->tm_mon += 1; break;
        case quarter_card: tm->tm_mon += 3; break;
        case year_card: tm->tm_year += 1; break;
        case times_card: tm->tm_year += 1; // 次卡默认1年有效期
    }
    member.expire_date = mktime(tm);
    
    // 保存会员信息
    if (add_user(member)) {
        show_message_dialog(GTK_WINDOW(current_window), "会员添加成功！");
        // 清空表单并生成新ID
        update_generated_id_label();
        gtk_entry_set_text(GTK_ENTRY(card_id_entry), "");
        gtk_entry_set_text(GTK_ENTRY(name_entry), "");
        // 重置日历到今天
        GDate *today = g_date_new();
        g_date_set_time_t(today, time(NULL));
        gtk_calendar_select_day(GTK_CALENDAR(birth_calendar), g_date_get_day(today));
        gtk_calendar_select_month(GTK_CALENDAR(birth_calendar), g_date_get_month(today)-1, g_date_get_year(today));
        g_date_free(today);
        gtk_entry_set_text(GTK_ENTRY(phone_entry), "");
        gtk_entry_set_text(GTK_ENTRY(balance_entry), "0");
        gtk_combo_box_set_active(GTK_COMBO_BOX(sex_combo), 0);
        gtk_combo_box_set_active(GTK_COMBO_BOX(type_combo), 0);
        gtk_combo_box_set_active(GTK_COMBO_BOX(status_combo), 0);
    } else {
        show_message_dialog(GTK_WINDOW(current_window), "添加失败：系统错误");
    }
}

// 修改按钮回调 - 打开修改对话框
static void on_modify_clicked(GtkButton *button, gpointer data) {
    const char* query_str = gtk_entry_get_text(GTK_ENTRY(query_input_entry));
    if (strlen(query_str) == 0) {
        show_message_dialog(GTK_WINDOW(current_window), "请先查询要修改的会员");
        return;
    }
    
    // 确定查询类型并找到用户
    int query_type = gtk_combo_box_get_active(GTK_COMBO_BOX(query_type_combo));
    user_imf user;
    gboolean found = FALSE;
    
    if (query_type == 0) { // 按ID查询
        unsigned int user_id = atoi(query_str);
        found = find_user_by_id(user_id, &user);
    } else if (query_type == 1) { // 按卡号查询
        found = find_user_by_card_id(query_str, &user);
    } else { // 按姓名查询
        GSList *results = find_users_by_name(query_str);
        if (results != NULL) {
            user = *(user_imf*)results->data;
            found = TRUE;
            // 释放内存
            while (results) {
                g_free(results->data);
                results = g_slist_delete_link(results, results);
            }
        }
    }
    
    if (!found) {
        show_message_dialog(GTK_WINDOW(current_window), "未找到该用户");
        return;
    }
    
    // 保存当前要修改的用户ID
    current_modify_id = user.user_id;
    
    // 创建修改对话框
    modify_dialog = gtk_dialog_new_with_buttons("修改会员信息",
                                              GTK_WINDOW(current_window),
                                              GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                              "取消", GTK_RESPONSE_CANCEL,
                                              "保存", GTK_RESPONSE_OK,
                                              NULL);
    gtk_window_set_default_size(GTK_WINDOW(modify_dialog), 500, 500);
    
    // 对话框内容区域
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(modify_dialog));
    GtkWidget *grid = gtk_grid_new();
    gtk_grid_set_row_spacing(GTK_GRID(grid), 15);
    gtk_grid_set_column_spacing(GTK_GRID(grid), 15);
    gtk_container_set_border_width(GTK_CONTAINER(grid), 20);
    gtk_box_pack_start(GTK_BOX(content_area), grid, TRUE, TRUE, 0);
    
    // 标题
    GtkWidget *title_label = gtk_label_new("修改会员信息");
    gtk_widget_set_name(title_label, "header");
    gtk_grid_attach(GTK_GRID(grid), title_label, 0, 0, 2, 1);
    gtk_widget_set_halign(title_label, GTK_ALIGN_CENTER);
    
    // 用户ID（不可修改）
    GtkWidget *id_label = gtk_label_new("用户ID:");
    gtk_widget_set_halign(id_label, GTK_ALIGN_END);
    GtkWidget *id_value_label = gtk_label_new(g_strdup_printf("%u", user.user_id));
    gtk_grid_attach(GTK_GRID(grid), id_label, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), id_value_label, 1, 1, 1, 1);
    
    // 注册时间（不可修改，完整显示年月日时分秒）
    GtkWidget *reg_date_label = gtk_label_new("注册时间:");
    gtk_widget_set_halign(reg_date_label, GTK_ALIGN_END);
    char reg_date_str[20];
    strftime(reg_date_str, sizeof(reg_date_str), "%Y-%m-%d %H:%M:%S", localtime(&user.reg_date));
    GtkWidget *reg_date_value_label = gtk_label_new(reg_date_str);
    gtk_grid_attach(GTK_GRID(grid), reg_date_label, 0, 2, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), reg_date_value_label, 1, 2, 1, 1);
    
    // 卡号
    GtkWidget *card_label = gtk_label_new("卡号:");
    gtk_widget_set_halign(card_label, GTK_ALIGN_END);
    modify_card_id_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(modify_card_id_entry), 10);
    gtk_entry_set_text(GTK_ENTRY(modify_card_id_entry), user.card_id);
    g_signal_connect(modify_card_id_entry, "changed", G_CALLBACK(on_card_id_changed), NULL);
    gtk_grid_attach(GTK_GRID(grid), card_label, 0, 3, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_card_id_entry, 1, 3, 1, 1);
    
    // 姓名
    GtkWidget *name_label = gtk_label_new("姓名:");
    gtk_widget_set_halign(name_label, GTK_ALIGN_END);
    modify_name_entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(modify_name_entry), user.user_name);
    gtk_grid_attach(GTK_GRID(grid), name_label, 0, 4, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_name_entry, 1, 4, 1, 1);
    
    // 性别
    GtkWidget *sex_label = gtk_label_new("性别:");
    gtk_widget_set_halign(sex_label, GTK_ALIGN_END);
    const gchar* sex_options[] = {"男", "女", NULL};
    modify_sex_combo = create_combo(sex_options);
    gtk_combo_box_set_active(GTK_COMBO_BOX(modify_sex_combo), user.sex == male ? 0 : 1);
    gtk_grid_attach(GTK_GRID(grid), sex_label, 0, 5, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_sex_combo, 1, 5, 1, 1);
    
    // 生日（日历选择器）
    GtkWidget *birth_label = gtk_label_new("生日:");
    gtk_widget_set_halign(birth_label, GTK_ALIGN_END);
    modify_birth_calendar = gtk_calendar_new();
    // 如果有生日信息，设置日历日期
    if (strlen(user.birth) == 10) {
        int year, month, day;
        if (sscanf(user.birth, "%d-%d-%d", &year, &month, &day) == 3) {
            gtk_calendar_select_month(GTK_CALENDAR(modify_birth_calendar), month - 1, year);
            gtk_calendar_select_day(GTK_CALENDAR(modify_birth_calendar), day);
        }
    }
    gtk_grid_attach(GTK_GRID(grid), birth_label, 0, 6, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_birth_calendar, 1, 6, 1, 1);
    
    // 电话
    GtkWidget *phone_label = gtk_label_new("电话:");
    gtk_widget_set_halign(phone_label, GTK_ALIGN_END);
    modify_phone_entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(modify_phone_entry), user.phone);
    gtk_grid_attach(GTK_GRID(grid), phone_label, 0, 7, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_phone_entry, 1, 7, 1, 1);
    
    // 会员类型
    GtkWidget *type_label = gtk_label_new("会员类型:");
    gtk_widget_set_halign(type_label, GTK_ALIGN_END);
    const gchar* type_options[] = {"次卡", "月卡", "季卡", "年卡", NULL};
    modify_type_combo = create_combo(type_options);
    gtk_combo_box_set_active(GTK_COMBO_BOX(modify_type_combo), user.user_type);
    gtk_grid_attach(GTK_GRID(grid), type_label, 0, 8, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_type_combo, 1, 8, 1, 1);
    
    // 余额/次数
    GtkWidget *balance_label = gtk_label_new("余额/次数:");
    gtk_widget_set_halign(balance_label, GTK_ALIGN_END);
    modify_balance_entry = gtk_entry_new();
    char balance_str[20];
    snprintf(balance_str, sizeof(balance_str), "%d", user.balance);
    gtk_entry_set_text(GTK_ENTRY(modify_balance_entry), balance_str);
    gtk_grid_attach(GTK_GRID(grid), balance_label, 0, 9, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_balance_entry, 1, 9, 1, 1);
    
    // 状态
    GtkWidget *status_label = gtk_label_new("状态:");
    gtk_widget_set_halign(status_label, GTK_ALIGN_END);
    const gchar* status_options[] = {"活跃", "冻结", "已删除", NULL};
    modify_status_combo = create_combo(status_options);
    gtk_combo_box_set_active(GTK_COMBO_BOX(modify_status_combo), user.status);
    gtk_grid_attach(GTK_GRID(grid), status_label, 0, 10, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), modify_status_combo, 1, 10, 1, 1);
    
    // 有效期（不可修改，根据类型自动计算）
    GtkWidget *expire_label = gtk_label_new("有效期至:");
    gtk_widget_set_halign(expire_label, GTK_ALIGN_END);
    char expire_date_str[20];
    strftime(expire_date_str, sizeof(expire_date_str), "%Y-%m-%d", localtime(&user.expire_date));
    GtkWidget *expire_value_label = gtk_label_new(expire_date_str);
    gtk_grid_attach(GTK_GRID(grid), expire_label, 0, 11, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), expire_value_label, 1, 11, 1, 1);
    
    // 连接保存按钮信号
    g_signal_connect(modify_dialog, "response", G_CALLBACK(on_modify_save_clicked), NULL);
    
    gtk_widget_show_all(modify_dialog);
}

// 保存修改的会员信息
static void on_modify_save_clicked(GtkButton *button, gpointer data) {
    // 如果是对话框响应信号
    if (G_TYPE_CHECK_INSTANCE_TYPE(button, GTK_TYPE_DIALOG)) {
        gint response = GPOINTER_TO_INT(data);
        if (response != GTK_RESPONSE_OK) {
            gtk_widget_destroy(modify_dialog);
            return;
        }
    }
    
    user_imf member = {0};
    member.user_id = current_modify_id;
    
    // 从表单获取数据
    const char* card_id = gtk_entry_get_text(GTK_ENTRY(modify_card_id_entry));
    const char* name = gtk_entry_get_text(GTK_ENTRY(modify_name_entry));
    int sex_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(modify_sex_combo));
    
    // 从日历选择器获取生日
    guint year, month, day;
    gtk_calendar_get_date(GTK_CALENDAR(modify_birth_calendar), &year, &month, &day);
    month++; // GTK的月份是0-11，需要转换为1-12
    
    char birth_str[11] = "";
    if (validate_birth_date(year, month, day)) {
        snprintf(birth_str, sizeof(birth_str), "%04d-%02d-%02d",
                 year, month, day);
    }
    
    const char* phone = gtk_entry_get_text(GTK_ENTRY(modify_phone_entry));
    int type_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(modify_type_combo));
    const char* balance_str = gtk_entry_get_text(GTK_ENTRY(modify_balance_entry));
    int status_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(modify_status_combo));
    
    // 验证必填字段
    if (strlen(name) == 0) {
        show_message_dialog(GTK_WINDOW(modify_dialog), "姓名为必填项！");
        return;
    }
    if (strlen(card_id) != 10) {
        show_message_dialog(GTK_WINDOW(modify_dialog), "卡号必须为10位！");
        return;
    }
    if (!is_card_id_unique(card_id, current_modify_id)) {
        show_message_dialog(GTK_WINDOW(modify_dialog), "卡号已存在！");
        return;
    }
    
    // 填充会员信息结构体
    strncpy(member.card_id, card_id, sizeof(member.card_id)-1);
    strncpy(member.user_name, name, sizeof(member.user_name)-1);
    member.sex = sex_idx == 0 ? male : female;
    strncpy(member.birth, birth_str, sizeof(member.birth)-1);
    strncpy(member.phone, phone, sizeof(member.phone)-1);
    
    // 保留原注册时间
    user_imf old_user;
    find_user_by_id(current_modify_id, &old_user);
    member.reg_date = old_user.reg_date;
    
    member.user_type = type_idx;
    member.balance = atoi(balance_str);
    member.status = status_idx;
    
    // 更新过期时间
    time_t now = time(NULL);
    struct tm* tm = localtime(&now);
    if (tm == NULL) {
        show_message_dialog(GTK_WINDOW(modify_dialog), "时间处理错误！");
        return;
    }
    
    switch (member.user_type) {
        case month_card: tm->tm_mon += 1; break;
        case quarter_card: tm->tm_mon += 3; break;
        case year_card: tm->tm_year += 1; break;
        case times_card: tm->tm_year += 1;
    }
    member.expire_date = mktime(tm);
    
    // 更新会员信息
    if (update_user(member)) {
        show_message_dialog(GTK_WINDOW(modify_dialog), "会员信息更新成功！");
        gtk_widget_destroy(modify_dialog);
        on_query_clicked(GTK_BUTTON(query_button), NULL);
    } else {
        show_message_dialog(GTK_WINDOW(modify_dialog), "更新失败：系统错误");
    }
}

// 查询会员回调（非static，可在其他地方调用）
void on_query_clicked(GtkButton *button, gpointer data) {
    const char* query_str = gtk_entry_get_text(GTK_ENTRY(query_input_entry));
    if (strlen(query_str) == 0) {
        show_message_dialog(GTK_WINDOW(current_window), "请输入查询内容");
        return;
    }
    
    // 确定查询类型
    int query_type = gtk_combo_box_get_active(GTK_COMBO_BOX(query_type_combo));
    GString *result = g_string_new("");
    
    // 表格标题（包含完整注册时间和有效期）
    g_string_append_printf(result, 
		    "%12s %22s %25s %15s %30s %30s %40s %25s %32s %15s %20s\n",
        "用户ID", "卡号", "姓名", "性别", "生日", "电话",
        "注册时间", "类型", "有效期至", "余额", "状态");
    g_string_append(result, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
    
    if (query_type == 0) { // 按ID查询
        unsigned int user_id = atoi(query_str);
        user_imf user;
        
        if (find_user_by_id(user_id, &user)) {
            char reg_date[20], expire_date[20];
            // 完整显示注册时间（年月日时分秒）
            strftime(reg_date, sizeof(reg_date), "%Y-%m-%d %H:%M:%S", localtime(&user.reg_date));
            strftime(expire_date, sizeof(expire_date), "%Y-%m-%d", localtime(&user.expire_date));
            
            g_string_append_printf(result,
					 "%-15u %-15s %-15s %-10s %-15s %-15s %-20s        %-15s %-15s %-10d %-10s\n",                               user.user_id, user.card_id, user.user_name,
                user.sex == male ? "男" : "女",
                strlen(user.birth) > 0 ? user.birth : "未填",
                strlen(user.phone) > 0 ? user.phone : "未填",
                reg_date,
                user.user_type == times_card ? "次卡" :
                user.user_type == month_card ? "月卡" :
                user.user_type == quarter_card ? "季卡" : "年卡",
                expire_date, user.balance,
                user.status == active ? "活跃" :
                user.status == frozen ? "冻结" : "已删除"
            );
        } else {
            g_string_append(result, "未找到该用户\n");
        }
    } else if (query_type == 1) { // 按卡号查询
        user_imf user;
        
        if (find_user_by_card_id(query_str, &user)) {
            char reg_date[20], expire_date[20];
            strftime(reg_date, sizeof(reg_date), "%Y-%m-%d %H:%M:%S", localtime(&user.reg_date));
            strftime(expire_date, sizeof(expire_date), "%Y-%m-%d", localtime(&user.expire_date));
            
            g_string_append_printf(result,
               		"%-15u %-15s %-15s %-10s %-15s %-15s %-20s        %-15s %-15s %-10d %-10s\n",       
	   				user.user_id, user.card_id, user.user_name,
                user.sex == male ? "男" : "女",
                strlen(user.birth) > 0 ? user.birth : "未填",
                strlen(user.phone) > 0 ? user.phone : "未填",
                reg_date,
                user.user_type == times_card ? "次卡" :
                user.user_type == month_card ? "月卡" :
                user.user_type == quarter_card ? "季卡" : "年卡",
                expire_date, user.balance,
                user.status == active ? "活跃" :
                user.status == frozen ? "冻结" : "已删除"
            );
        } else {
            g_string_append(result, "未找到该用户\n");
        }
    } else { // 按姓名查询
        GSList *results = find_users_by_name(query_str);
        
        if (results == NULL) {  // 修正笔误：N#include -> NULL
            g_string_append(result, "未找到匹配的用户\n");
        } else {
            char reg_date[20], expire_date[20];
            GSList *iter = results;
            int count = 0;
            
            while (iter) {
                user_imf *user = (user_imf*)iter->data;
                strftime(reg_date, sizeof(reg_date), "%Y-%m-%d %H:%M:%S", localtime(&user->reg_date));
                strftime(expire_date, sizeof(expire_date), "%Y-%m-%d", localtime(&user->expire_date));
                
                g_string_append_printf(result,
						"%-15u %-15s %-15s %-10s %-15s %-15s %-20s        %-15s %-15s %-10d %-10s\n",  
                                     user->user_id, user->card_id, user->user_name,
                    user->sex == male ? "男" : "女",
                    strlen(user->birth) > 0 ? user->birth : "未填",
                    strlen(user->phone) > 0 ? user->phone : "未填",
                    reg_date,
                    user->user_type == times_card ? "次卡" :
                    user->user_type == month_card ? "月卡" :
                    user->user_type == quarter_card ? "季卡" : "年卡",
                    expire_date, user->balance,
                    user->status == active ? "活跃" :
                    user->status == frozen ? "冻结" : "已删除"
                );
                
                iter = iter->next;
                count++;
            }
            
            g_string_append_printf(result, "\n共找到 %d 位匹配的会员\n", count);
            
            // 释放内存
            while (results) {
                g_free(results->data);
                results = g_slist_delete_link(results, results);
            }
        }
    }
    
    gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(query_result_text)), result->str, -1);
    g_string_free(result, TRUE);
}

// 删除会员回调
static void on_delete_clicked(GtkButton *button, gpointer data) {
    const char* query_str = gtk_entry_get_text(GTK_ENTRY(query_input_entry));
    if (strlen(query_str) == 0) {
        show_message_dialog(GTK_WINDOW(current_window), "请输入查询内容");
        return;
    }
    
    // 确定查询类型并找到用户ID
    int query_type = gtk_combo_box_get_active(GTK_COMBO_BOX(query_type_combo));
    user_imf user;
    gboolean found = FALSE;
    
    if (query_type == 0) { // 按ID查询
        unsigned int user_id = atoi(query_str);
        found = find_user_by_id(user_id, &user);
    } else if (query_type == 1) { // 按卡号查询
        found = find_user_by_card_id(query_str, &user);
    } else { // 按姓名查询
        GSList *results = find_users_by_name(query_str);
        if (results != NULL) {
            user = *(user_imf*)results->data;
            found = TRUE;
            // 释放内存
            while (results) {
                g_free(results->data);
                results = g_slist_delete_link(results, results);
            }
        }
    }
    
    if (!found) {
        show_message_dialog(GTK_WINDOW(current_window), "未找到该用户");
        return;
    }
    
    // 确认删除对话框
    GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(current_window),
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_WARNING,
                                             GTK_BUTTONS_YES_NO,
                                             "确定要删除ID为 %u 的会员吗？", user.user_id);
    gint response = gtk_dialog_run(GTK_DIALOG(dialog));
    if (response == GTK_RESPONSE_YES) {
        del_user(user.user_id);
        show_message_dialog(GTK_WINDOW(current_window), "会员删除成功");
        gtk_entry_set_text(GTK_ENTRY(query_input_entry), "");
        gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(query_result_text)), "", -1);
    }
    gtk_widget_destroy(dialog);
}

// 列出所有会员回调
static void on_list_all_clicked(GtkButton *button, gpointer data) {
    user_imf *users;
    int count;
    list_all(&users, &count);
    
    if (count == 0) {
        show_message_dialog(GTK_WINDOW(current_window), "暂无会员数据");
        gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(list_text_view)), "", -1);
        return;
    }
    
    GString *result = g_string_new("");
    g_string_append_printf(result, 
         "%12s %22s %25s %15s %30s %40s %30s %25s %42s %15s %20s\n",
        "用户ID", "卡号", "姓名", "性别", "生日", "电话",
        "注册时间", "类型", "有效期至", "余额", "状态");
    g_string_append(result, "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
    char reg_date[20], expire_date[20];
    for (int i = 0; i < count; i++) {
        // 完整显示注册时间（年月日时分秒）
        strftime(reg_date, sizeof(reg_date), "%Y-%m-%d %H:%M:%S", localtime(&users[i].reg_date));
        strftime(expire_date, sizeof(expire_date), "%Y-%m-%d", localtime(&users[i].expire_date));
        
        g_string_append_printf(result,
			"%-15u %-25s %-20s %-10s  %-25s %-30s %-30s   %-25s %-15s %-10d %-10s\n", 

                     users[i].user_id, users[i].card_id, users[i].user_name,
            users[i].sex == male ? "男" : "女",
            strlen(users[i].birth) > 0 ? users[i].birth : "未填",
            strlen(users[i].phone) > 0 ? users[i].phone : "未填",
            reg_date,
            users[i].user_type == times_card ? "次卡" :
            users[i].user_type == month_card ? "月卡" :
            users[i].user_type == quarter_card ? "季卡" : "年卡",
            expire_date, users[i].balance,
            users[i].status == active ? "活跃" :
            users[i].status == frozen ? "冻结" : "已删除"
        );
    }
    
    g_string_append_printf(result, "\n共 %d 位会员\n", count);
    gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(list_text_view)), result->str, -1);
    
    g_string_free(result, TRUE);
    free(users);
}

// 刷新球桌显示 - 修正函数声明，参数类型为GtkWidget*
void refresh_table_display(GtkWidget *grid) {
    if (!GTK_IS_GRID(grid)) return;
    
    // 清除现有子控件（保留标题行）
    GList *children = gtk_container_get_children(GTK_CONTAINER(grid));
    for (GList *l = children; l != NULL; l = l->next) {
        static int title_count = 0;
        if (title_count < 5) {  // 标题列数量
            title_count++;
            continue;
        }
        gtk_widget_destroy(GTK_WIDGET(l->data));
    }
    g_list_free(children);

    // 其余代码保持不变...
}

// on_table_start_clicked函数中修复调用
void on_table_start_clicked(GtkWidget *widget, gpointer data) {
    // ... 现有代码 ...
    
    // 确保hall_grid已正确初始化并传递
    if (hall_grid && GTK_IS_GRID(hall_grid)) {
        refresh_table_display(hall_grid);
    }
}

// on_table_end_clicked函数中修复调用
void on_table_end_clicked(GtkWidget *widget, gpointer data) {
    // ... 现有代码 ...
    
    if (hall_grid && GTK_IS_GRID(hall_grid)) {
        refresh_table_display(hall_grid);
    }
}

// on_table_maintenance_clicked函数中修复调用
void on_table_maintenance_clicked(GtkWidget *widget, gpointer data) {
    // ... 现有代码 ...
    
    if (hall_grid && GTK_IS_GRID(hall_grid)) {
        refresh_table_display(hall_grid);
    }
}

// show_hall_status_page函数中修复调用
void show_hall_status_page(GtkWidget *parent) {
    // ... 现有代码 ...
    
    // 初始化hall_grid的代码应该在这里
    // hall_grid = gtk_grid_new();
    
    if (hall_grid && GTK_IS_GRID(hall_grid)) {
        refresh_table_display(hall_grid);
    }
}

// 显示主窗口
void show_main_window(GtkApplication *app, const char *username) {
    current_window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(current_window), "野马俱乐部台球会员管理系统");
    gtk_window_set_default_size(GTK_WINDOW(current_window), 1150, 700);
    gtk_window_set_position(GTK_WINDOW(current_window), GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(current_window), 15);
    
    // 标题栏显示当前登录用户
    char title[100];
    snprintf(title, sizeof(title), "台球会员管理系统 - 当前登录: %s", username);
    gtk_window_set_title(GTK_WINDOW(current_window), title);
    
    // 加载CSS样式
    GtkCssProvider *css = gtk_css_provider_new();
    gtk_css_provider_load_from_data(css,
        "GtkNotebook tab { padding: 10px 20px; font-weight: bold; font-size: 10pt; }"
        "GtkNotebook tab:checked { background-color: white; border-bottom: 3px solid #3498db; }"
        ".header { font-size: 14pt; font-weight: bold; color: #2c3e50; margin: 15px 0; }"
        ".required { color: #e74c3c; font-weight: bold; }"
        ".submit-button { background-color: #2ecc71; color: white; padding: 10px 24px; border-radius: 5px; }"
        ".submit-button:hover { background-color: #27ae60; }"
        ".query-button { background-color: #3498db; color: white; padding: 8px 18px; border-radius: 5px; }"
        ".modify-button { background-color: #f39c12; color: white; padding: 8px 18px; border-radius: 5px; }"
        ".delete-button { background-color: #e74c3c; color: white; padding: 8px 18px; border-radius: 5px; }"
        ".list-button { background-color: #9b59b6; color: white; padding: 10px 24px; border-radius: 5px; }"
        "GtkEntry { padding: 8px; border-radius: 5px; border: 1px solid #bdc3c7; }"
        "GtkTextView { font-family: Monospace; font-size: 9pt; padding: 10px; }",
        -1, NULL);
    gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
                                             GTK_STYLE_PROVIDER(css),
                                             GTK_STYLE_PROVIDER_PRIORITY_USER);
    g_object_unref(css);
    
    GtkWidget *notebook = gtk_notebook_new();
    gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
    
    // 1. 添加会员标签页
    GtkWidget *add_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_set_border_width(GTK_CONTAINER(add_tab), 10);
    
    // 添加标题
    GtkWidget *add_header = gtk_label_new("添加新会员");
    gtk_widget_set_name(add_header, "header");
    gtk_box_pack_start(GTK_BOX(add_tab), add_header, FALSE, FALSE, 5);
    
    // 表单容器
    GtkWidget *form_grid = gtk_grid_new();
    gtk_grid_set_row_spacing(GTK_GRID(form_grid), 18);
    gtk_grid_set_column_spacing(GTK_GRID(form_grid), 20);
    gtk_box_pack_start(GTK_BOX(add_tab), form_grid, FALSE, FALSE, 10);
    
    // 系统生成的用户ID
    GtkWidget *id_label = gtk_label_new("用户ID:");
    gtk_widget_set_halign(id_label, GTK_ALIGN_END);
    generated_id_label = gtk_label_new("");
    gtk_widget_set_halign(generated_id_label, GTK_ALIGN_START);
    gtk_grid_attach(GTK_GRID(form_grid), id_label, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), generated_id_label, 1, 1, 1, 1);
    update_generated_id_label();
    
    // 卡号（10位，必填）
    GtkWidget *card_label = gtk_label_new("卡号:");
    gtk_widget_set_halign(card_label, GTK_ALIGN_END);
    card_id_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(card_id_entry), 10);
    gtk_entry_set_placeholder_text(GTK_ENTRY(card_id_entry), "请输入10位卡号");
    g_signal_connect(card_id_entry, "changed", G_CALLBACK(on_card_id_changed), NULL);
    gtk_grid_attach(GTK_GRID(form_grid), card_label, 0, 2, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), card_id_entry, 1, 2, 1, 1);
    
    // 姓名（必填）
    GtkWidget *name_label = gtk_label_new("姓名:");
    gtk_widget_set_halign(name_label, GTK_ALIGN_END);
    name_entry = gtk_entry_new();
    gtk_entry_set_placeholder_text(GTK_ENTRY(name_entry), "请输入姓名");
    gtk_grid_attach(GTK_GRID(form_grid), name_label, 0, 3, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), name_entry, 1, 3, 1, 1);
    
    // 性别
    GtkWidget *sex_label = gtk_label_new("性别:");
    gtk_widget_set_halign(sex_label, GTK_ALIGN_END);
    const gchar* sex_options[] = {"男", "女", NULL};
    sex_combo = create_combo(sex_options);
    gtk_grid_attach(GTK_GRID(form_grid), sex_label, 0, 4, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), sex_combo, 1, 4, 1, 1);
    
    // 生日（日历选择器）
    GtkWidget *birth_label = gtk_label_new("生日:");
    gtk_widget_set_halign(birth_label, GTK_ALIGN_END);
    birth_calendar = gtk_calendar_new();
    gtk_grid_attach(GTK_GRID(form_grid), birth_label, 0, 5, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), birth_calendar, 1, 5, 1, 1);
    
    // 电话
    GtkWidget *phone_label = gtk_label_new("电话:");
    gtk_widget_set_halign(phone_label, GTK_ALIGN_END);
    phone_entry = gtk_entry_new();
    gtk_entry_set_placeholder_text(GTK_ENTRY(phone_entry), "例如: 13800138000");
    gtk_grid_attach(GTK_GRID(form_grid), phone_label, 0, 6, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), phone_entry, 1, 6, 1, 1);
    
    // 会员类型
    GtkWidget *type_label = gtk_label_new("会员类型:");
    gtk_widget_set_halign(type_label, GTK_ALIGN_END);
    const gchar* type_options[] = {"次卡", "月卡", "季卡", "年卡", NULL};
    type_combo = create_combo(type_options);
    gtk_grid_attach(GTK_GRID(form_grid), type_label, 0, 7, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), type_combo, 1, 7, 1, 1);
    
    // 余额/次数
    GtkWidget *balance_label = gtk_label_new("余额/次数:");
    gtk_widget_set_halign(balance_label, GTK_ALIGN_END);
    balance_entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(balance_entry), "0");
    gtk_grid_attach(GTK_GRID(form_grid), balance_label, 0, 8, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), balance_entry, 1, 8, 1, 1);
    
    // 状态
    GtkWidget *status_label = gtk_label_new("状态:");
    gtk_widget_set_halign(status_label, GTK_ALIGN_END);
    const gchar* status_options[] = {"活跃", "冻结", "已删除", NULL};
    status_combo = create_combo(status_options);
    gtk_grid_attach(GTK_GRID(form_grid), status_label, 0, 9, 1, 1);
    gtk_grid_attach(GTK_GRID(form_grid), status_combo, 1, 9, 1, 1);
    
    // 提交按钮
    GtkWidget *submit_button = gtk_button_new_with_label("添加会员");
    gtk_widget_set_size_request(submit_button, 140, 40);
    gtk_widget_set_name(submit_button, "submit-button");
    g_signal_connect(submit_button, "clicked", G_CALLBACK(on_member_submit), NULL);
    
    // 按钮容器
    GtkWidget *submit_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_widget_set_halign(submit_box, GTK_ALIGN_CENTER);
    gtk_box_pack_start(GTK_BOX(submit_box), submit_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(add_tab), submit_box, FALSE, FALSE, 20);
    
    // 2. 查询/修改/删除会员标签页
    GtkWidget *query_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_set_border_width(GTK_CONTAINER(query_tab), 10);
    
    // 标题
    GtkWidget *query_header = gtk_label_new("查询/修改/删除会员");
    gtk_widget_set_name(query_header, "header");
    gtk_box_pack_start(GTK_BOX(query_tab), query_header, FALSE, FALSE, 10);
    
    // 查询区域容器
    GtkWidget *query_container = gtk_grid_new();
    gtk_grid_set_row_spacing(GTK_GRID(query_container), 15);
    gtk_grid_set_column_spacing(GTK_GRID(query_container), 15);
    gtk_container_set_border_width(GTK_CONTAINER(query_container), 20);
    gtk_box_pack_start(GTK_BOX(query_tab), query_container, FALSE, FALSE, 10);
    
    // 查询类型选择
    GtkWidget *query_type_label = gtk_label_new("查询类型:");
    gtk_widget_set_halign(query_type_label, GTK_ALIGN_END);
    const gchar* query_options[] = {"按用户ID", "按卡号", "按姓名", NULL};
    query_type_combo = create_combo(query_options);
    gtk_grid_attach(GTK_GRID(query_container), query_type_label, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(query_container), query_type_combo, 1, 1, 1, 1);
    
    // 查询输入框
    GtkWidget *query_input_label = gtk_label_new("查询内容:");
    gtk_widget_set_halign(query_input_label, GTK_ALIGN_END);
    query_input_entry = gtk_entry_new();
    gtk_entry_set_placeholder_text(GTK_ENTRY(query_input_entry), "请输入要查询的内容");
    gtk_grid_attach(GTK_GRID(query_container), query_input_label, 0, 2, 1, 1);
    gtk_grid_attach(GTK_GRID(query_container), query_input_entry, 1, 2, 1, 1);
    
    // 查询按钮（全局指针赋值）
    query_button = gtk_button_new_with_label("查询");
    gtk_widget_set_name(query_button, "query-button");
    g_signal_connect(query_button, "clicked", G_CALLBACK(on_query_clicked), NULL);
    
    // 修改按钮
    GtkWidget *modify_button = gtk_button_new_with_label("修改");
    gtk_widget_set_name(modify_button, "modify-button");
    g_signal_connect(modify_button, "clicked", G_CALLBACK(on_modify_clicked), NULL);
    
    // 删除按钮
    GtkWidget *delete_button = gtk_button_new_with_label("删除");
    gtk_widget_set_name(delete_button, "delete-button");
    g_signal_connect(delete_button, "clicked", G_CALLBACK(on_delete_clicked), NULL);
    
    // 按钮容器
    GtkWidget *button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(button_box), query_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(button_box), modify_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(button_box), delete_button, FALSE, FALSE, 0);
    gtk_grid_attach(GTK_GRID(query_container), button_box, 1, 3, 1, 1);
    
    // 结果显示区域
    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scrolled_window), 450);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
    gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 5);
    gtk_box_pack_start(GTK_BOX(query_tab), scrolled_window, TRUE, TRUE, 5);
    
    query_result_text = gtk_text_view_new();
    gtk_text_view_set_editable(GTK_TEXT_VIEW(query_result_text), FALSE);
    gtk_container_add(GTK_CONTAINER(scrolled_window), query_result_text);
    
    // 3. 列出所有会员标签页
    GtkWidget *list_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_set_border_width(GTK_CONTAINER(list_tab), 10);
    
    // 标题
    GtkWidget *list_header = gtk_label_new("所有会员列表");
    gtk_widget_set_name(list_header, "header");
    gtk_box_pack_start(GTK_BOX(list_tab), list_header, FALSE, FALSE, 5);
    
    // 列出所有按钮
    GtkWidget *list_button = gtk_button_new_with_label("列出所有会员");
    gtk_widget_set_size_request(list_button, 160, 40);
    gtk_widget_set_name(list_button, "list-button");
    g_signal_connect(list_button, "clicked", G_CALLBACK(on_list_all_clicked), NULL);
    gtk_box_pack_start(GTK_BOX(list_tab), list_button, FALSE, FALSE, 5);
    
    // 列表显示区域
    GtkWidget *list_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(list_scrolled_window), 500);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_scrolled_window), GTK_SHADOW_IN);
    gtk_container_set_border_width(GTK_CONTAINER(list_scrolled_window), 5);
    gtk_box_pack_start(GTK_BOX(list_tab), list_scrolled_window, TRUE, TRUE, 5);
    
    list_text_view = gtk_text_view_new();
    gtk_text_view_set_editable(GTK_TEXT_VIEW(list_text_view), FALSE);
    gtk_container_add(GTK_CONTAINER(list_scrolled_window), list_text_view);
    
    // 添加标签页
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), add_tab, gtk_label_new("添加会员"));
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), query_tab, gtk_label_new("查询/修改会员"));
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), list_tab, gtk_label_new("所有会员"));
    
    gtk_container_add(GTK_CONTAINER(current_window), notebook);
    gtk_widget_show_all(current_window);
}

